package com.gu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.exception.DiyException;
import com.gu.entity.AclPermission;
import com.gu.entity.vo.PermissionVo;
import com.gu.service.AclPermissionService;
import com.gu.mapper.AclPermissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class AclPermissionServiceImpl extends ServiceImpl<AclPermissionMapper, AclPermission>
        implements AclPermissionService {

    //递归获取菜单，包括一级，二级，三级菜单；
    @Override
    public PermissionVo getAll() {
        // pid为0 mysql数据库中的表示为 顶层父系目录：
        PermissionVo permissionVo = new PermissionVo();
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", "0");
        AclPermission aclPermission = this.getOne(wrapper);
        if (StringUtils.isEmpty(aclPermission)) {
            throw new DiyException("777", "父系菜单为空!");
        } else {
            BeanUtils.copyProperties(aclPermission, permissionVo);
            permissionVo.setLevel(1);
            List<PermissionVo> childrenPermissionList = this.getChildrenPermission(permissionVo);
            permissionVo.setChildren(childrenPermissionList);
        }

        return permissionVo;
    }

    //递归查询得到父系菜单下的所有的子菜单；
    private List<PermissionVo> getChildrenPermission(PermissionVo parentPermissionVo) {
        //临时存放子菜单的列表
        List<PermissionVo> childrenList = new ArrayList<>();

        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", parentPermissionVo.getId());
        List<AclPermission> list = this.list(wrapper);
        //终止条件
        if (list.size() > 0) {
            list.forEach(aclPermission -> {
                PermissionVo childrenPermissionVo = new PermissionVo();
                BeanUtils.copyProperties(aclPermission, childrenPermissionVo);
                //层级+1
                childrenPermissionVo.setLevel(parentPermissionVo.getLevel() + 1);
                //递归
                List<PermissionVo> childrenPermissionList = this.getChildrenPermission(childrenPermissionVo);
                childrenPermissionVo.setChildren(childrenPermissionList);
                childrenList.add(childrenPermissionVo);
            });
        }
        return childrenList;
    }

    //递归删除菜单；
    @Override
    public boolean deleteById(String id) {
        ArrayList<String> idList = new ArrayList<>();
        idList.add(id);

        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        wrapper.select("id");
        List<AclPermission> list = this.list(wrapper);

        if (list.size() > 0) {
            list.forEach(aclPermission -> {
                String parentId = aclPermission.getId();
                idList.add(parentId);
                this.getChildrenIdList(parentId, idList);
            });
        } else {
            throw new DiyException("888", "id不存在");
        }

        int result = baseMapper.deleteBatchIds(idList);

        return result > 0 ? true : false;
    }

    //获取递归删除的idList；
    private void getChildrenIdList(String id, ArrayList<String> idList) {

        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        wrapper.select("id");
        List<AclPermission> childrenList = this.list(wrapper);
        if (childrenList.size() > 0) {

            childrenList.forEach(child -> {
                String parentId = child.getId();
                idList.add(parentId);
                this.getChildrenIdList(parentId, idList);
            });
        }
    }


}




